package com.sm.honkon.ui.model

import android.os.CountDownTimer
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import au.com.safie.user.app.util.CacheUtil
import com.blankj.utilcode.util.ToastUtils
import com.sm.honkon.app.App
import com.sm.honkon.app.network.apiService
import com.sm.honkon.app.utils.SendCodeHelper
import com.sm.honkon.data.bean.HandlerEntity
import com.sm.honkon.data.bean.UserInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.ext.util.notNull
import kotlin.random.Random

class SignupViewModel:BaseViewModel() {
    val user = MutableLiveData<UserInfo>(UserInfo("", ""))
    val countDownTip = StringObservableField("获取验证码")
    val isCountDown = BooleanObservableField(false)
    val myCode = StringObservableField()
    val code = Random.nextInt(9999)
    val countDownTimer = object : CountDownTimer(60 * 1000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            isCountDown.set(true)
            countDownTip.set("重新发送(${millisUntilFinished / 1000L})")
        }

        override fun onFinish() {
            isCountDown.set(false)
            countDownTip.set("获取验证码")
        }

    }

    fun reset(backCall: (HandlerEntity) -> Unit) {
        user.value?.let { userInfo ->
            if (userInfo.mobile.isEmpty()) {
                ToastUtils.showShort("请输入手机号/工号")
                return@let
            }
            if (myCode.get() != code.toString()) {
                ToastUtils.showShort("请输入正确的验证码")
                return@let
            }
            viewModelScope.launch(Dispatchers.Main) {
                val handleDao = App.database.handleDao()
                val handlers =
                    withContext(Dispatchers.Default) {
                        handleDao.getHandlersList()
                    }
                handlers.find {
                    it.mobile == userInfo.mobile
                }.notNull(
                    {
                        if (it.status == 1) {
                            ToastUtils.showShort("用户已被禁用")
                        } else {
                            backCall(it)
                        }
                    }, {
                        ToastUtils.showShort("账号不存在")
                    }
                )
            }
        }

    }

    fun login(back: () -> Unit) {
        user.value?.let {
            if (it.mobile.isEmpty()) {
                ToastUtils.showShort("请输入手机号/工号")
                return@let
            }
            if (it.password.isEmpty()) {
                ToastUtils.showShort("请输入密码")
                return@let
            }
            if (myCode.get() != code.toString()) {
                ToastUtils.showShort("请输入正确的验证码")
                return@let
            }
            CacheUtil.setUser(it)
            back.invoke()
        }
    }

    fun clear() {
        countDownTimer.cancel()
    }

    fun getSmsCode() {
        user.value?.let {
            if (it.mobile.isEmpty()) {
                ToastUtils.showShort("请输入手机号/工号")
                return@let
            }
            request(
                { apiService.getCode(SendCodeHelper.createUrl(it.mobile, code.toString())) },
                {})
            countDownTimer.start()
        }
    }
}